// pages/amount/amount.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index:0,
        index:0,
        isUseMoney:0,
        moneyArr:['￥4.71'],
        freeArr:['快递免邮'],
        explain:'',
        menuList:[],
        addressList:[],
        nums:0,
        sumPrice:0,
        items:[
            {name:'微信',value:'1',checked:true}
        ],
        allsumPrice:0,
        decmoney:0,
        amount:0,
    },
    ///获取默认地址信息
    getAddressList(){
        wx.get('http://localhost:8000/wx/shopping/address').then(res => {
          console.log('ad',res)
          this.setData({
            addressList:res
          })
        })
      },
    ///获取订单信息
    getMenuList(){
        //获取购物车数据
        wx.post('http://localhost:8000/wx/getShopingCart',{memberId:wx.getStorageSync('userInfo')._id,cartId:wx.getStorageSync('cart')}).then(res => {
        if(res.code == 200){
            this.setData({
                menuList:res.data
            })
        }
        })
        this.setData({
            nums:wx.getStorageSync('nums'),
            sumPrice:Number(wx.getStorageSync('sumPrice')),
            amount:parseFloat((Number(wx.getStorageSync('sumPrice'))-Number(this.data.decmoney)).toFixed(2))
        })
    },
    //选择优惠券
    money(){
        wx.navigateTo({
          url: '/pages/selectCoupon/selectCoupon',
        })
    },
    ///支付选择
    selectpay:function(e){ 
        // console.log('radio发生change事件，携带value值为：', e.detail.value)
    },

    ///修改地址信息
    boult(){
        wx.navigateTo({
          url: '/pages/mine/mine',
        })
    },
    ///配送服务选择
    pick:function(e){
        // console.log('picker发送选择改变，携带值为', )
        this.setData({
            index:e.detail.value
        })
    },
    ///运费险选择
    pick1:function(e){
        // console.log('picker发送选择改变，携带值为', )
        this.setData({
            index1:e.detail.value
        })
    },
    input(e){
        let data = {}
        data[e.currentTarget.dataset.key] = e.detail.value
        this.setData(data)
      },
      //获取减免金额
      getDecmoney(){
          let couponId = wx.getStorageSync('couponId')
          if(couponId){
            wx.get('http://localhost:8000/wx/paySelectCoupon?couponId='+couponId).then(res => {
                if(res.code == 200){
                    this.setData({
                        decmoney:Number(res.data.num),
                        amount:parseFloat((this.data.sumPrice-Number(res.data.num)).toFixed(2))
                    })
                }
            })
          }
      },
      ///提交订单
      getmenu(){
        var that =this
        let allPrice = (this.data.amount).toString()
        wx.showModal({
            title: '订单确认提醒',
            content: '是否提交订单？',
            success(res) {
              if (res.confirm) {
                  wx.showModal({
                    title: '支付确认',
                    content:'请支付'+allPrice+'元',
                    success(a){
                        if(a.confirm){
                            wx.post('http://localhost:8000/wx/submitOrder',{id:wx.getStorageSync('userInfo')._id,couponId:wx.getStorageSync('couponId') ||null,cartIds:wx.getStorageSync('cart'),nums:wx.getStorageSync('nums'),addressId:that.data.addressList[0]._id}).then(b => {
                                console.log('bbbbb',b)
                                if(b.code == 200){
                                    wx.showToast({
                                        title: '支付成功',
                                        icon:'success',
                                        duration: 2000
                                      })
                                      setTimeout(() =>{
                                        wx.navigateBack({
                                            delta:-1,
                                        })
                                      },2000)
                                      wx.removeStorageSync('cart')
                                      wx.removeStorageSync('nums')
                                      wx.removeStorageSync('sumPrice')
                                }
                            })
                        }
                    }
                  })
              }
            }
          })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMenuList()
        this.getAddressList()
        this.getDecmoney()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getDecmoney()
        // this.getAddressList()

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.removeStorageSync('couponId')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})